package sicnu.cs.ich.log.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sicnu.cs.ich.api.domain.entity.LoginLog;
import sicnu.cs.ich.api.domain.entity.OperationLog;
import sicnu.cs.ich.common.common.ObjectToSqlFileExecutor;
import sicnu.cs.ich.log.mapper.LogMapper;
import sicnu.cs.ich.log.rest.form.LogPageParam;
import sicnu.cs.ich.log.rest.form.LoginPageParam;
import sicnu.cs.ich.log.service.ILogService;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author CaiKe
 * @date 2021/12/7 19:14
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LogServiceImpl implements ILogService {

    private final LogMapper logMapper;
    private final SqlSession sqlSession;

    private ObjectToSqlFileExecutor<OperationLog> operLogExecutor;
    private ObjectToSqlFileExecutor<LoginLog> loginExecutor;

    @PostConstruct
    public void init(){
        operLogExecutor = new ObjectToSqlFileExecutor<>(OperationLog.class, sqlSession, "operationLog");
        loginExecutor = new ObjectToSqlFileExecutor<>(LoginLog.class, sqlSession, "loginLog");
    }

    @Override
    public PageInfo<OperationLog> getOperationLog(LogPageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        final List<OperationLog> logs = logMapper.listOperationLogs(pageParam);
        if (logs == null || logs.isEmpty()) {
            throw new NoSuchElementException("暂无数据！");
        }
        return new PageInfo<>(logs);
    }

    @Override
    public PageInfo<LoginLog> getLoginLog(LoginPageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        final List<LoginLog> logs = logMapper.listLoginLogs(pageParam);
        if (logs == null || logs.isEmpty()) {
            throw new NoSuchElementException("暂无数据！");
        }
        return new PageInfo<>(logs);
    }


    @Override
    public void tempStoreOperationLog(OperationLog operLog) throws InterruptedException {
        operLogExecutor.tempStoreLog(operLog);
    }

    @Override
    public void tempStoreLoginLog(LoginLog loginLog) throws InterruptedException {
        loginExecutor.tempStoreLog(loginLog);
    }

    @Override
    public void saveOperationLogs() throws IOException {
        operLogExecutor.saveLogs();
    }

    @Override
    public void saveLoginLogs() throws IOException {
        loginExecutor.saveLogs();
    }

    @Override
    public void closeStream() throws IOException {
        operLogExecutor.close();
        loginExecutor.close();
    }

    @Override
    public void deleteOperationSQL(Long seconds) {
        operLogExecutor.cleanOldSQLFiles(seconds);
    }

    @Override
    public void deleteLoginSQL(Long seconds) {
        operLogExecutor.cleanOldSQLFiles(seconds);
    }


}